#include <stdlib.h>
#include <stdio.h>
#include <string.h>


static void mem_leak1(void)

{

    char *p = malloc(1);

    // 释放
    free(p);

}


static void mem_leak2(void)

{

    FILE *fp = fopen("test.txt", "w");

}


static void mem_overrun1(void)

{

    // char *p = malloc(1);

    char *p = malloc(2);

    *(short*)p = 2;


    free(p);

}


static void mem_overrun2(void)

{

    char array[5];

    strcpy(array, "hello");

}


static void mem_double_free(void)

{

    char *p = malloc(1);

    free(p);

    // free(p);

}


static void mem_free_wild_pointer(void)

{

    char *p;

    // 申请空间
    p = malloc(16);
    // 初始化
    strcpy(p, "hello valgrind!");

    free(p);

}



int main()
{

    mem_leak1();

    mem_leak2();

    mem_overrun1();

    mem_overrun2();

    mem_double_free();

    mem_free_wild_pointer();


    return 0;
}